package com.demon.minio.utils;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @author: Mr.wu
 * @date: 2022年07月29日 10:15
 */
public class OkHttpClientUtils {

    public static int doPut(String url, byte[] bytes) {
        OkHttpClient client = new OkHttpClient().newBuilder() //
                .readTimeout(60, TimeUnit.SECONDS) // 设置读取超时时间
                .writeTimeout(60, TimeUnit.SECONDS) // 设置写的超时时间
                .connectTimeout(60, TimeUnit.SECONDS) // 设置连接超时时间
                .build();

        MediaType mediaType = MediaType.parse("*/**; charset=utf-8");
        RequestBody body = RequestBody.create(bytes, mediaType);
        Request request = new Request.Builder().url(url).put(body).build();
        try (Response response = client.newCall(request).execute()) {
            System.out.println(response.body().string());
            return response.code();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 500;
    }
}
